package com.gears_software.sxdc.dccx.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.gears_software.sxdc.dccx.R;
import com.gears_software.sxdc.dccx.application.DCCXApplication;
import com.gears_software.sxdc.dccx.data.finder.IMemberFinder;
import com.gears_software.sxdc.dccx.data.finder.RequestUrl;
import com.gears_software.sxdc.dccx.data.finder.util.GearsErrorHandler;
import com.gears_software.sxdc.dccx.data.model.MemberModel;
import com.gears_software.sxdc.dccx.data.model.ResultModel;
import com.gears_software.sxdc.dccx.ui.util.MaterialDialogProgress;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.App;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.rest.spring.annotations.RestService;

import qiu.niorgai.StatusBarCompat;

/**
 * Created by SunMuran on 2017/8/3.
 */
@EActivity(R.layout.activity_my_information)
public class MyInformationActivity extends Activity {
    @App
    DCCXApplication app;
    @ViewById
    TextView actionbar_title;
    @ViewById
    TextView back_icon;
    @ViewById
    TextView right_icon;
    @ViewById
    TextView tv_shiming_icon;
    @ViewById
    TextView tv_icon1;
    @ViewById
    TextView tv_icon2;
    @ViewById
    TextView tv_chezhu_icon;
    @ViewById
    RelativeLayout rl_shiming;
    @ViewById
    RelativeLayout rl_chezhu;
    @ViewById
    SimpleDraweeView img_avatar;
    @ViewById
    TextView tv_nicheng;
    @ViewById
    TextView tv_phone_number;
    @ViewById
    TextView tv_shiming_yirenzheng;
    @ViewById
    TextView tv_chezhu_yirenzheng;
    @Bean
    GearsErrorHandler errorHandler;
    @RestService
    IMemberFinder memberFinder;

    MaterialDialogProgress progress;
    MemberModel memberModel;

    @AfterViews
    void created() {
        actionbar_title.setText("我的");
        initIconfont();
        StatusBarCompat.setStatusBarColor(this,ContextCompat.getColor(this, R.color.colorActionbar));

        memberFinder.setRestErrorHandler(errorHandler);
        progress = new MaterialDialogProgress(this);
        progress.show("正在加载...");
        getCurrentMember();
    }

    void initIconfont() {
        tv_shiming_icon.setTypeface(app.iconfont);
        tv_icon1.setTypeface(app.iconfont);
        tv_icon2.setTypeface(app.iconfont);
        tv_chezhu_icon.setTypeface(app.iconfont);
        back_icon.setTypeface(app.iconfont);
        back_icon.setText(R.string.back_icon);
        right_icon.setText("编辑");
        right_icon.setTextSize(16);
    }

    @Background
    void getCurrentMember() {
        ResultModel<MemberModel> result = memberFinder.getMember();
        showMemberResult(result);
    }

    @UiThread
    void showMemberResult(ResultModel<MemberModel> res) {
        if (res != null) {
            if (res.getErrno().equals("0")) {
                if (res.getData() != null) {
                    memberModel = res.getData();
                    if (memberModel.getAvatar() != null && !memberModel.getAvatar().equals("")) {
                        Uri uri = Uri.parse(memberModel.getAvatar() + RequestUrl.avatar);
                        img_avatar.setImageURI(uri);
                    }
                    if (memberModel.getNickname() != null && !memberModel.getNickname().equals("")) {
                        tv_nicheng.setText(memberModel.getNickname());
                    } else {
                        tv_nicheng.setText("大昌车主");
                    }
                    tv_phone_number.setText(memberModel.getDriverphone());
                    switch (memberModel.getPassengerauditstate()) {
                        case "1":
                            tv_icon2.setTextColor(ContextCompat.getColor(this, R.color.colorFontGray));
                            tv_shiming_yirenzheng.setText("末认证");
                            break;
                        case "2":
                            tv_icon2.setTextColor(ContextCompat.getColor(this, R.color.colorFontOrange));
                            tv_shiming_yirenzheng.setText("认证中");
                            break;
                        case "3":
                            tv_icon2.setText(R.string.shibai_icon);
                            tv_icon2.setTextColor(ContextCompat.getColor(this, R.color.colorActionbar));
                            tv_shiming_yirenzheng.setText("认证失败");
                            break;
                        case "4":
                            tv_icon2.setTextColor(ContextCompat.getColor(this, R.color.colorGreen));
                            tv_shiming_yirenzheng.setText("已认证");
                            break;
                    }
                    switch (memberModel.getCarpooldriverauditstate()) {
                        case "1":
                            tv_icon1.setTextColor(ContextCompat.getColor(this, R.color.colorFontGray));
                            tv_chezhu_yirenzheng.setText("末认证");
                            break;
                        case "2":
                            tv_icon1.setTextColor(ContextCompat.getColor(this, R.color.colorFontOrange));
                            tv_chezhu_yirenzheng.setText("认证中");
                            break;
                        case "3":
                            tv_icon1.setText(R.string.shibai_icon);
                            tv_icon1.setTextColor(ContextCompat.getColor(this, R.color.colorActionbar));
                            tv_chezhu_yirenzheng.setText("认证失败");
                            break;
                        case "4":
                            tv_icon1.setTextColor(ContextCompat.getColor(this, R.color.colorGreen));
                            tv_chezhu_yirenzheng.setText("已认证");
                            break;
                    }

                }
            }
        }
        progress.dismiss();
    }

    @Click
    void right_icon() {
        Intent intent = new Intent(this, BianJiActivity_.class);
        intent.putExtra("memberModel", memberModel);
        startActivity(intent);
    }

    @Click
    void back_icon() {
        finish();
    }

    @Click
    void rl_shiming() {
        if (memberModel != null) {
            switch (memberModel.getPassengerauditstate()) {
                case "1":
                    goShiMing();
                    break;
                case "2":
                    shiMingWan();
                    break;
                case "3":
                    goShiMing();
                    break;
                case "4":
                    shiMingWan();
                    break;
            }
        }
    }

    private void goShiMing() {
        Intent intent = new Intent(this, ShiMingActivity_.class);
        startActivity(intent);
    }

    private void shiMingWan() {
        Intent intent = new Intent(this, SMRZTGActivity_.class);
        startActivity(intent);
    }

    @Click
    void rl_chezhu() {
        if (memberModel != null) {
            switch (memberModel.getCarpooldriverauditstate()) {
                case "1":
                    goJiaShiZheng();
                    break;
                case "2":
                    JSZtongguo();
                    break;
                case "3":
                    goJiaShiZheng();
                    break;
                case "4":
                    JSZtongguo();
                    break;
            }
        }
    }


    private void goJiaShiZheng() {
        Intent intent = new Intent(this, JiaShiZhengActivity_.class);
        startActivity(intent);
    }

    private void JSZtongguo() {
        Intent intent = new Intent(this, CZRZTGActivity_.class);
        startActivity(intent);
    }

    @Override
    protected void onResume() {
        super.onResume();
        getCurrentMember();
    }
}
